<?php

define("TEMPLATE_DIR", CA_ROOT."templates/");

class TemplateHandler
{
	private $exists = false;
	
	private $template;
	
	private $protected_vars = array();
	
	public function __construct($template, $tpldir = false)
	{
		$_tpldir = TEMPLATE_DIR;
		
		if($tpldir != false)
		{
			$_tpldir = $tpldir;
		}
		
		if(!file_exists($_tpldir.$template))
			return false;
		
		$this->protected_vars = get_class_vars("TemplateHandler");
		$this->template = $_tpldir.$template;
		$this->exists = true;
		
		$this->assign("TEMPLATE_DIR", substr($_tpldir, strlen($_SERVER['DOCUMENT_ROOT'])));
		
		return true;
	}
	
	public function display()
	{
		if($this->exists)
			require $this->template;
		
		return false;
	}
	
	public function source()
	{
		if($this->exists)
		{
			return file_get_contents($this->template);
		}
		
		return false;
	}
	
	public function assign($key, $value)
	{
		if(!in_array($key, $this->protected_vars) && is_string($key))
		{
			$this->$key = $value;
			return true;
		}
		
		return false;
	}
	
	public function get($key)
	{
		if(!in_array($key, $this->protected_vars) && is_string($key))
			return $this->$key;
		
		return false;
	}
}

?>